package com.tong.hao.fw.service.controller.test;

import cn.buddha.jec.util.TongBeanUtils;
import cn.buddha.jec.util.TongBusinessUtils;
import com.tong.hao.ac.api.feign.query.IQueryUserInfoFeign;
import com.tong.hao.ac.api.feign.test.ITestFeign;
import com.tong.hao.ac.api.model.in.query.QueryUserInfoIn;
import com.tong.hao.ac.api.model.out.query.QueryUserInfoOut;
import com.tong.hao.common.annotation.HaoValidate;
import com.tong.hao.common.exception.HaoExceptionEnum;
import com.tong.hao.fw.api.model.test.FwDemoReq;
import com.tong.hao.fw.api.model.test.FwDemoRes;
import com.tong.hao.fw.api.service.test.IFwTestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TestController
 * @Author 友野浩二
 * @Description fw模块接口测试
 * @Version 1.0
 */
@Api(tags = "fw测试")
@RestController
@Slf4j
public class FwTestController implements IFwTestController {

    @Autowired
    private IQueryUserInfoFeign iQueryUserInfoFeign;

    @Autowired
    private ITestFeign iTestFeign;

    @ApiOperation(value = "fw测试方法")
    @RequestMapping(method = RequestMethod.POST, value = TEST_QUERY)
    @HaoValidate
    @Override
    public FwDemoRes fwTestQuery(@RequestBody FwDemoReq fwDemoReq) {
        log.info("com.tong.hao.fw.service.controller.test.FwTestController.fwTestQuery starting...");

        QueryUserInfoIn queryUserInfoIn = new QueryUserInfoIn();
        queryUserInfoIn.setFullName("fw用户");
        queryUserInfoIn.setMailbox("fw@163.com");
        queryUserInfoIn.setTongInHead(fwDemoReq.getTongInHead());
        log.info("fw模块通过feign调用ac模块的restful接口/query/user/info请求报文, queryUserInfoIn: {}", queryUserInfoIn);
        QueryUserInfoOut queryUserInfoOut = iQueryUserInfoFeign.queryUserInfo(queryUserInfoIn);
        log.info("fw模块通过feign调用ac模块的restful接口/query/user/info响应报文, queryUserInfoOut: {}", queryUserInfoOut);

        if (TongBeanUtils.isNullObj(queryUserInfoOut.getTongOutHead())) {
            throw TongBusinessUtils.createBusinessException(HaoExceptionEnum.FEIGN_FAIL.getCode(), HaoExceptionEnum.FEIGN_FAIL.getMessage());
        }

        FwDemoRes fwDemoRes = new FwDemoRes();
        fwDemoRes.setFwResCeShi("呵呵");

        return fwDemoRes;
    }

    @Override
    @ApiOperation(value = "fw测试")
    @RequestMapping(method = RequestMethod.GET, value = TEST_INFO)
    public String test() {
        return this.iTestFeign.haha(null) + ", fw测试";
//        return null;
    }
}
